package com.imy.lua.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.util.ColorUtils;
import com.imy.lua.R;

public class FindColorView extends BaseCustomViewGroup {
    private TextView logTv;
    private View colorView;
    private CropRectView cropRectView;
    private View findPointView;
    public FindColorView(Context context) {
        super(context);
    }

    public FindColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FindColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(View inflate) {
        colorView = inflate.findViewById(R.id.color_view);
        findPointView =inflate.findViewById(R.id.find_point_view);
        logTv = inflate.findViewById(R.id.log_tv);
        cropRectView =inflate.findViewById(R.id.find_color_crop_rect_view);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                tryToGetColor(event);
                return true;
            }
        });
    }

    private void tryToGetColor(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int rawX = (int) event.getRawX();
                int rawY = (int) event.getRawY();
                if (listener != null) {
                    listener.onMove(rawX, rawY);
                }
                break;
            case MotionEvent.ACTION_UP:
                upX = (int) event.getRawX();
                upY = (int) event.getRawY();
                if (listener != null) {
                    listener.onUp(upX, upY);
                }
                break;
        }
    }

    private int upX;
    private int upY;
    @Override
    protected int getLayoutID() {
        return R.layout.custom_view_manual_color;
    }

    public void show() {
        setVisibility(VISIBLE);
    }

    public void showColor(int color, int x, int y) {
        String s = ColorUtils.integerToHex(color);
        logTv.setText("当前颜色:" + s + " x:" + x + " y:" + y+" 精度:"+currentDim);
        colorView.setBackgroundColor(color);
    }

    public void hide() {
        setVisibility(GONE);
    }
    private String currentColor;
    public void setCurrentColor(int color, int x, int y) {
        String s = ColorUtils.integerToHex(color);
        currentColor=s;
        logTv.setText("当前颜色:" + s + " x:" + x + " y:" + y+" 精度:"+currentDim);
        colorView.setBackgroundColor(color);
    }

    public int getCropWidth() {
        cropRectView.measure(0, 0);
        return cropRectView.getMeasuredWidth();
    }
    public int getCropHeight(){
        cropRectView.measure(0, 0);
        return cropRectView.getMeasuredHeight();
    }
    public int getCropX(){
       return (int) cropRectView.getX();
    }
    public int getCropY(){
        return (int) cropRectView.getY();
    }

    public String getCurrentColor() {
        return currentColor;
    }

    public void showFindPoint(int x,int y) {
        findPointView.setVisibility(VISIBLE);
        findPointView.setX(x);
        findPointView.setY(y);
    }

    public void setDim(float dim) {
        this.currentDim=dim;
    }
    private float currentDim;

    public float getCurrentDim() {
        return currentDim;
    }

    public interface ColorTouchListener {
        void onMove(int x, int y);

        void onUp(int x, int y);
    }

    public void setColorTouchListener(ColorTouchListener manualListener) {
        this.listener = manualListener;
    }

    private ColorTouchListener listener;
}
